11. Template Method

Template Method

ND079 JPND C2 L03 A12 Template Method

What is the Template Method Pattern?

  • You define a base class or interface for a procedure or algorithm, but leave empty placeholders for some parts of the procedure.
  • Each placeholder is a blank or default method in the base class.
  • This base class acts as a template.
  • Callers fill in the blanks by extending the base class and overriding the placeholder methods.

Which SOLID design principles apply to the template method pattern?

SOLUTION:
  • Open-Closed Principle
  • Liskov Substitution Principle
  • Dependency Inversion Principle

QUIZ QUESTION::

Match each behavioral design pattern with its description.

ANSWER CHOICES:



Description

Pattern

Different implementations of a task interface can be swapped at runtime.

Subclasses extend a base class and override different placeholder methods.

A "subject" notifies one or more subscribers that something has changed about the subject.

SOLUTION:

Description

Pattern

A "subject" notifies one or more subscribers that something has changed about the subject.

Subclasses extend a base class and override different placeholder methods.

Different implementations of a task interface can be swapped at runtime.